let Browser = (function() {
    function Browser() { }
    /**
     * 获取浏览器数据
     */
    Browser.getBrowser = function() {
        let UA = navigator.userAgent || '';
        let isAndroid = (function() {
            return !!UA.match(/Android/i);
        })();
        let isQQ = (function() {
            return /(iPad|iPhone|iPod).*? (IPad)?QQ\/([\d\.]+)/.test(UA) || /\bV1_AND_SQI?_([\d\.]+)(.*? QQ\/([\d\.]+))?/.test(UA);
        })();
        let isIOS = (function() {
            return !!UA.match(/iPhone|iPad|iPod/i);
        })();
        let isSafari = (function() {
            return /iPhone|iPad|iPod\/([\w.]+).*(safari).*/i.test(UA);
        })();
        let isWx = (function() {
            return !!UA.match(/micromessenger/i);
        })();
        let isWb = (function() {
            return !!UA.match(/weibo/i);
        })();
        let isAndroidChrome = (function() {
            return (UA.match(/Chrome\/([\d.]+)/) || UA.match(/CriOS\/([\d.]+)/)) && isAndroid && !isQQ;
        })();
        let isQZ = (function() {
            return UA.indexOf('Qzone/') !== -1;
        })();
        let browser = {
            isAndroid: isAndroid,
            isIOS: isIOS,
            isSafari: isSafari,
            isQQ: isQQ,
            isWb: isWb,
            isWx: isWx,
            isQZ: isQZ,
            isAndroidChrome: isAndroidChrome
        };
        return browser;
    };
    return Browser;
})();
/**
 * splitUrl: 分割url
 */
export const splitUrl = function() {
    let b = [];
    let e = location.href;
    if (e.indexOf('?') !== -1) {
        let f = e.substring(e.indexOf('?') + 1);
        let a = f.split('&');
        for (let d = 0; d < a.length; d++) {
            let c = a[d].indexOf('=');
            if (c !== -1) {
                if (!b[a[d].substring(0, c)]) {
                    if (a[d].substring(c + 1).indexOf('#') > -1) {
                        b[a[d].substring(0, c)] = a[d].substring(c + 1).replace(/[#/]/g, '');
                        continue;
                    }
                    b[a[d].substring(0, c)] = a[d].substring(c + 1);
                }
            }
        }
    }
    return b;
};
/**
 * AppLink:H5唤起APP的所有方法
 */
export const AppLink = (function() {
    /**
     * 类的contructor方法
     * @param config IAppLink类型的config文件
     */
    function AppLink(config) {
        /**
         * UA
         */
        this.UA = {};
        /**
         * 传入的config数据，以接口约束
         */
        this.config = {};
        this.UA = navigator.userAgent || '';
        this.config = config;
    }
    /**
     * 跳转函数
     * @param url 链接
     */
    AppLink.prototype.go = function(url) {
        setTimeout(() => {
            window.location.href = url;
        }, 3000);
    };
    /**
     * 检查是否唤起
     * @param cb 回调函数
     */
    AppLink.prototype.checkOpen = function(cb) {
        let inter = null;
        let statue = false;
        let count = 0;
        inter = window.setInterval(function() {
            count++;
            statue = document.hidden || document.webkitHidden;
            if (statue || count > 40) {
                cb(statue);
                clearInterval(inter);
            }
        }, 50);
    };
    /**
     * 外部调用的入口函数
     */
    AppLink.prototype.open = function() {
        let _this = this;
        let browser = Browser.getBrowser();
        let config = this.config;
        // 微信直接跳 应用宝
        if (browser.isWx) {
            this.go(this.config.yyb);
        } else if (browser.isIOS) {
            this.tryCallApp(this.config.schema);
            // 没有下载
            this.checkOpen(function(isSuccess) {
                if (!isSuccess) {
                    _this.go(_this.config.appstore);
                }
            });
        } else if (browser.isAndroid) {
            // iOS跳到AppStore
            // 使用scheme唤起
            this.tryCallApp(this.config.schema);
            // 唤起失败 跳到应用宝
            this.checkOpen(function(isSuccess) {
                console.log(isSuccess);
                if (!isSuccess) {
                    _this.go(_this.config.yyb);
                }
            });
        } else if (browser.isWb) {
            // 使用scheme唤起
            this.tryCallApp(this.config.schema);
            // 微博：唤起失败，也不跳转，会有引导功能
        } else if (browser.isSafari) {
            // var version = this.getIOSVersion()
            // iOS10以下不支持直接跳转到AppStore，跳到应用宝
            this.tryCallApp(this.config.schema);
            if (version < 10) {
                this.go(this.config.yyb);
            } else {
                this.tryCallApp(this.config.schema);
                this.checkOpen(function(isSuccess) {
                    if (!isSuccess) {
                        _this.go(_this.config.appstore);
                    }
                });
            }
        } else {
            // 其他情况，直接跳应用宝
            this.go(this.config.yyb);
        }
    };
    /**
     * 下载按钮的url地址
     */
    AppLink.prototype.getDownloadUrl = function() {
        let browser = Browser.getBrowser();
        let config = this.config;
        let url = '';
        if (browser.isQQ) {
            if (browser.isIOS) {
                url = this.config.appstore;
            } else {
                url = this.config.yyb;
            }
        } else if (browser.isSafari) {
            let version = this.getIOSVersion();
            if (version < 10) {
                url = this.config.yyb;
            } else {
                url = this.config.appstore;
            }
        } else if (browser.isWb) {
            url = '';
        } else {
            url = this.config.yyb;
        }
        return url;
    };
    /**
     * 尝试唤起APP
     * @param scheme 唤起的scheme
     */
    AppLink.prototype.tryCallApp = function(scheme) {
        let aLink = document.createElement('a'),
            body = document.body;
        aLink.href = scheme;
        body.appendChild(aLink);
        aLink.click();
    };
    /**
     * 判断iOS版本
     */
    AppLink.prototype.getIOSVersion = function() {
        let ver = navigator.appVersion.match(/OS (\d+)_(\d+)_?(\d+)?/);
        let version = parseInt(ver[1], 10);
        return version;
    };
    return AppLink;
})();
